/**
 * @file parser.c
 * Betriebssysteme Parser File.
 * 
 * Beispiel 1
 *
 * @author Markus Scharitzer 1410258040 <markus.scharitzer@technikum-wien.at>
 * @date 2015/03/12
 *
 * @version 1.0 
 *
 * Handles Filenames
 *
 */
/*
 * -------------------------------------------------------------- includes --
 */

#include <fnmatch.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "parser.h"

/*
 * --------------------------------------------------------------- defines --
 */

/*
 * -------------------------------------------------------------- typedefs --
 */

/*
 * --------------------------------------------------------------- globals --
 */

/*
 * ------------------------------------------------------------- functions --
 */

/**
 *
 * \brief check_name function
 *
 * checks if name of entry matches parms
 *
 * \param argv for error handling
 * \param name name and path of the current entry
 * \param parms pattern to match
 *
 * \return bool match 1 nomatch 0
 *
 */

bool check_name(const char * const * argv,char* name, const char* parms){
  bool result = false;
  char* newname = basename(name);
  if( (result = fnmatch(parms, newname,0))){
    if(result==FNM_NOMATCH);
    else {
      fprintf(stderr,"%s :Unexpected Error: %s\n ", argv[0],strerror(errno));
      exit(1);
    }
  }

  return !result;

}
/**
 *
 * \brief check_path function
 *
 * checks if path of entry matches parms
 *
 * \param argv for error handling
 * \param name name and path of the current entry
 * \param parms pattern to match
 *
 * \return bool match 1 nomatch 0
 *
 */
bool check_path(const char * const * argv,char *name,const char *parms)
{
  bool result = false;
  if( (result = fnmatch(parms, name, 0))){
    if(result==FNM_NOMATCH);
    else {
      fprintf(stderr,"%s :Unexpected Error: %s\n ", argv[0],strerror(errno));
      exit(1);
    }
  }
  
  return !result;
}
